//
//  HMComposeView.m
//  微博app--OC
//
//  Created by xmy on 16/9/7.
//  Copyright © 2016年 xmy. All rights reserved.
//

#import "HMComposeView.h"
#import "UIImage+XYImage.h"
#import "HMComposeButton.h"
#import "Masonry.h"
#import "POP.h"

@interface HMComposeView ()

//背景截屏的图片
@property(nonatomic,strong)UIImageView *bgImage;
//上面文字图片
@property(nonatomic,strong)UIImageView *sloganImage;
//存放模型数组的信息
@property(nonatomic,strong)NSArray *infoArray;
//存放子按钮的数组
@property(nonatomic,strong)NSMutableArray *buttons;
//外界传过来的控制器
@property(nonatomic,strong)UIViewController *target;
@end
@implementation HMComposeView

- (instancetype)initWithFrame:(CGRect)frame;
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI{
    
    //设置view的大小
    CGRect xframe = self.frame;
    
    xframe.size = [UIScreen mainScreen].bounds.size;
    
    //初始化控件
    self.bgImage = [[UIImageView alloc]initWithImage:[UIImage getScreenSnap]];
    self.sloganImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"compose_slogan"]];
    
    //构建约束
    [self.bgImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    [self.sloganImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(self).offset(100);
    }];
    
    [self addChildButtons];

}

//添加子控件按钮
- (void)addChildButtons{

    //按钮的宽度
    CGFloat itemW = 80;
    //按钮的高度
    CGFloat itemH = 110;
    
    CGFloat w = [UIScreen mainScreen].bounds.size.width;
    //间距
    CGFloat itemMargin = (w - 3 * itemW) / 4;
    
    //解析plist数据
    NSString *path = [[NSBundle mainBundle] pathForResource:@"compose.plist" ofType:nil];
    
    NSArray *arr = [NSArray arrayWithContentsOfFile:path];
    //存放模型
    self.infoArray = arr;
    
    //遍历这个数组
    for(int i = 0; i < arr.count ; i++){
        
        //定义一个button
        HMComposeButton *btn = [[HMComposeButton alloc]init];
        
        [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        
        //取出对应位置的字典
        //设置图标及文字
        [btn setImage:[UIImage imageNamed:arr[i][@"icon"]] forState:UIControlStateNormal];
        
        [btn setTitle:arr[i][@"title"] forState:UIControlStateNormal];
        
        //设置大小
        CGSize xSize = btn.frame.size;
        
        xSize = CGSizeMake(itemW, itemH);
        
        //第几列
        NSInteger col = i % 3;
        //第几行
        NSInteger row = i / 3;
        
        //通过列数求出x值
        CGFloat x = col * itemW + (col + 1) * itemMargin;
        
        //通过行数求出y值
        CGFloat y = row * itemH + w;
        
        //设置按钮的位置
        CGPoint p = btn.frame.origin;
        
        p = CGPointMake(x, y);
        
        [self addSubview:btn];
        
        [self.buttons addObject:btn];
    }

}


//将当前空间显示出来,供外界调用
- (void)show:(UIViewController *)target{
    
    self.target = target;
    //将view添加到穿过来的控制器上
    [self.target.view addSubview:self];
    
    //将控件显示出来,执行动画
    for(int i = 0;i<self.buttons.count;i++){
        
        [self anim:self.buttons[i] andIndex:i isUp:true];
        
    }
    
}

//执行按钮的动画
- (void)anim:(UIButton *)button andIndex:(int)index isUp:(BOOL)isUp{
    
    POPSpringAnimation *anim = [[POPSpringAnimation alloc]init];
    
    //*********
    [anim pop_animationForKey:kPOPViewCenter];
     anim.toValue = [NSValue valueWithCGPoint:CGPointMake(button.center.x, button.center.y + (isUp? -350 : 350))];
    
    anim.springSpeed = 10;
    
    anim.springBounciness = 12;
    
    //动画开始执行时间
    anim.beginTime = CACurrentMediaTime() + index * 0.025;
    
    [button pop_addAnimation:anim forKey:nil];
    
}

//点击屏幕消失
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    //将控件显示出来,执行动画
    for(int i = 0;i<self.buttons.count;i++){
        
        [self anim:self.buttons[i] andIndex:i isUp:false];
        
    }
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self removeFromSuperview];
    });
    
    

}
@end
